<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible="dialogVisible"
    append-to-body
    title="发布任务清单"
    width="90%"
    top="5vh"
    @close="$emit('close')"
  >
    <!--工具栏-->
    <div class="crud-group">
      <!-- 搜索 -->
      <el-select
        v-model="query.publishClusterIds"
        clearable
        placeholder="选择集群搜索"
        class="filter-item"
      >
        <el-option
          v-for="item in clusterLists"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="query.publishState"
        clearable
        placeholder="选择状态搜索"
        class="filter-item"
      >
        <el-option
          v-for="item in publishTaskState"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-input
        v-model="query.publishUser"
        clearable
        size="small"
        placeholder="发布用户"
        style="width:200px"
        class="filter-item"
      />
      <rrOperation :crud="crud" />
    </div>
    <crudOperation :permission="permission" />
    <!--表格渲染-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      max-height="350px"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        v-if="columns.visible('publishClusterNames')"
        label="发布集群名"
        prop="publishClusterNames"
      />
      <el-table-column
        label="发布/还原动作"
        prop="publishState"
        width="120px"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.publishState==='PUBLISHING'">发布中</el-tag>
          <el-tag
            v-if="scope.row.publishState==='PUBLISHED'"
            type="success"
          >发布完成</el-tag>
          <el-tag v-if="scope.row.publishState==='RESTORING'">还原中</el-tag>
          <el-tag
            v-if="scope.row.publishState==='RESTORED'"
            type="info"
          >还原完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columns.visible('publishUser')"
        label="发布用户名"
        prop="publishUser"
      />
      <el-table-column
        v-if="columns.visible('publishState')"
        label="进度"
        prop="publishState"
        width="240px"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.publishState === 'PUBLISHING' || scope.row.publishState === 'PUBLISHED'">
            <el-tag
              style="margin: 0 5px"
              type="info"
            >总数: {{ scope.row.publishTotal }}</el-tag>
            <el-tag
              style="margin: 0 5px"
              type="success"
            >成功: {{ scope.row.publishSuccess }}</el-tag>
            <el-tag
              style="margin: 0 5px"
              type="danger"
            >失败: {{ scope.row.publishFail }}</el-tag>
          </div>
          <div v-if="scope.row.publishState === 'RESTORING' || scope.row.publishState === 'RESTORED'">
            <el-tag
              style="margin: 0 5px"
              type="info"
            >总数: {{ scope.row.restoreTotal }}</el-tag>
            <el-tag
              style="margin: 0 5px"
              type="success"
            >成功: {{ scope.row.restoreSuccess }}</el-tag>
            <el-tag
              style="margin: 0 5px"
              type="danger"
            >失败: {{ scope.row.restoreFail }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        v-if="columns.visible('dateFlag')"
        label="时间节点"
        prop="dateFlag"
      />
      <el-table-column
        v-if="columns.visible('createBy')"
        label="创建人"
        prop="createBy"
      />
      <el-table-column
        v-if="columns.visible('createTime')"
        label="创建时间"
        prop="createTime"
      />
      <el-table-column label="扫描明细">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="lookPublishTaskList(scope.row.id)"
          >查看发布清单</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <!--分页组件-->
      <pagination />
    </div>

    <el-dialog
      v-if="lookPublishListFlag"
      :close-on-click-modal="false"
      :before-close="cancelCU"
      :visible.sync="lookPublishListFlag"
      append-to-body
      title="发布清单"
      width="1200px"
      top="10vh"
    >
      <publish-list :publish-task-id="this.publishTaskId" />
    </el-dialog>
  </el-dialog>
</template>

<script>
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/extend/script/publish/CRUD.taskOperation'
import pagination from '@crud/Pagination'
import clusterSparingApi from '@/api/cluster/clusterSparing'
import scriptPublishTaskApi from '@/api/script/publish/scriptPublishTask'
import publishList from '@/views/scriptPublish/publishList/index'

// crud交由presenter持有
const defaultCrud = CRUD({ title: '脚本扫描确认清单', url: 'api/scriptPublish/task/page', crudMethod: { ...scriptPublishTaskApi }})
const defaultForm = {
}
export default {
  name: 'ScriptPublishTask',
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  components: { rrOperation, crudOperation, pagination, publishList},
  props: {
    publishVersionId: {
      type: String,
      default: null
    },
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      table: null,
      permission: {
        add: ['admin', 'scriptPack:add'],
        publish: ['admin', 'scriptPack:publish'],
        del: ['admin', 'scriptPack:del']
      },
      clusterLists: [],
      publishTaskId: null,
      lookPublishListFlag: false,
      publishTaskState: [
        { key: 'PUBLISHING', value: '发布中' },
        { key: 'PUBLISHED', value: '发布完成' },
        { key: 'RESTORING', value: '还原中' },
        { key: 'RESTORED', value: '还原完成' }
      ]
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        this.page = 0
        this.crud.resetQuery()
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.publishVersionId = this.publishVersionId
    },
    initData() {
      this.crud.resetQuery(true)
      clusterSparingApi.findListByPage().then(res => {
        res.records.forEach((array) => {
          this.clusterLists.push({ key: array.id, value: array.clusterName })
        })
      })
    },
    lookPublishTaskList(id) {
      this.lookPublishListFlag = true
      this.publishTaskId = id
    },
    cancelCU() {
      this.lookPublishListFlag = false
    }
  }
}
</script>
<style scoped>

</style>
